package controller;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
import java.io.IOException;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

/**
 *
 * @author ronaldomartins
 */
@ManagedBean
@RequestScoped
public class ControllerIndex {

    private String usuario;
    private String senha;
    
    private String erro;

    public void setSenha(String senha) {
        this.senha = senha;
    }

    public String getSenha() {
        return senha;
    }

    public void setUsuario(String usuario) {
        this.usuario = usuario;
    }

    public String getUsuario() {
        return usuario;
    }

    public void setErro(String erro) {
        this.erro = erro;
    }

    public String getErro() {
        return erro;
    }
    
    
    
    public void login() throws IOException{
        if (this.usuario.equals("admin") && this.senha.equals("123")){
            FacesContext.getCurrentInstance().getExternalContext().redirect("app/inicial.xhtml"); 
            //or   return "newPage" + "?faces-redirect=true"
        }else{
            FacesContext.getCurrentInstance().addMessage("", new FacesMessage(FacesMessage.SEVERITY_ERROR,"Usuário não encontrado",""));
        }
//        Colocando o objeto na sessão 
        FacesContext fc = FacesContext.getCurrentInstance();  
        HttpSession session = (HttpSession) fc.getExternalContext().getSession(true);  
        session.setAttribute("usuario", this.usuario );  
        
//        Pegando o objeto da sessão 
//        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();  
//        HttpSession session = (HttpSession) request.getSession();  
//        Cliente c = session.getAttribute("clienteAux")); 
        
    }
    
}
